using System;
using System.Diagnostics;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using AgilePlannerDT.Utils;
using Microsoft.Win32;

namespace AgilePlannerDT
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
  
    public partial class App : System.Windows.Application
    {
        public App()
        {
            InitializeComponent();

            var msSurfaceRegistryLocator = new MicrosoftSurfaceVersionLocator();
            var surfaceVersion = msSurfaceRegistryLocator.findLatestVersion();
            var surfaceRegistry = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Surface\" + surfaceVersion,
                                                    "IsPhysicalSurfaceUnit", -1);

            //Application not running on the MS Surface, load normal window app)
            if (surfaceRegistry == null)
            {
                this.StartupUri = new Uri(@"AgilePlannerMainWindow.xaml", UriKind.Relative);
            }
            //Application is not running on the MS Surface, but the Surface SDK is installed, load normal window app
            else if (surfaceRegistry != null && (int)surfaceRegistry == 0)
            {
                if (IsMSSurfaceShellRunning())
                {
                    //The Surface emulator is running, load the surface window to appear in the emulator
                    this.StartupUri = new Uri(@"Window/AgilePlannerMicrosoftSurfaceWindow.xaml", UriKind.Relative);
                }
                else
                {
                    // else just load the normal window
                    this.StartupUri = new Uri(@"AgilePlannerMainWindow.xaml", UriKind.Relative);
                }
                
            }
                //Application is running on the MS Surface hardware, load the Surface window
            else if ((int)surfaceRegistry == 1)
            {
                this.StartupUri = new Uri(@"Window/AgilePlannerMicrosoftSurfaceWindow.xaml", UriKind.Relative);
            }
        }

        private bool IsMSSurfaceShellRunning()
        {
            var procs = Process.GetProcesses();
            foreach (var process in procs)
            {
                if(process.ProcessName.Contains("SurfaceShell")) return true;
            }
            return false;
        }

    }
}